package com.sushi.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sushi.exception.BusinessException;
import com.sushi.repository.entity.ActivityApplyEntity;
import com.sushi.repository.mapper.ActivityApplyMapper;
import com.sushi.service.ActivityApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sushi.response.ResponseBO;
import com.sushi.response.ResponsePageBO;
import com.sushi.response.ActivityApplyResp;
import com.sushi.request.ActivityApplyReq;
import com.sushi.request.ActivityApplyPageReq;
import com.sushi.utils.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动报名 服务实现类
 * @date 2023-05-02
 * @time 17:09:50
 * @author Administrator
 */
@Slf4j
@Service
public class ActivityApplyServiceImpl extends ServiceImpl<ActivityApplyMapper, ActivityApplyEntity> implements ActivityApplyService {

	@Autowired
	private ActivityApplyMapper activityApplyMapper;

    

    /**
     * 新增
     */
    @Override
    public ResponseBO<ActivityApplyResp> add(ActivityApplyReq req) {
        ActivityApplyEntity entity = BeanUtil.convert(req, ActivityApplyEntity.class);
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("保存失败");
        }
        entity.insert();
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityApplyResp.class));
    }

    /**
     * 逻辑删除
     */
    @Override
    public ResponseBO<ActivityApplyResp> logicRemove(ActivityApplyReq req) {
        if (StringUtil.isBlank(req.getId())) {
            throw new BusinessException("id不能为空");
        }
        ActivityApplyEntity entity = new ActivityApplyEntity();
        entity.setId(req.getId());
        entity.setDeleted(1L);
        entity.updateById();
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityApplyResp.class));
    }

    /**
     * 更新
     */
    @Override
    public ResponseBO<ActivityApplyResp> updateByField(ActivityApplyReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        ActivityApplyEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            return ResponseUtil.error("信息不存在，无法更新");
        }
        if (StringUtil.isNotBlank(req.getId())) {
            entity.setId(req.getId());
        }
        if (StringUtil.isNotBlank(req.getActivityId())) {
            entity.setActivityId(req.getActivityId());
        }
        if (StringUtil.isNotBlank(req.getUserId())) {
            entity.setUserId(req.getUserId());
        }
        if (StringUtil.isNotBlank(req.getStatus())) {
            entity.setStatus(req.getStatus());
        }

        entity.updateById();
        log.info("更新成功：{}", JSON.toJSONString(entity));
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityApplyResp.class));
    }

    /**
     * 列表
     */
    @Override
    public ResponsePageBO<ActivityApplyResp> listByCondition(ActivityApplyPageReq req) {
        if (ObjectUtil.isEmpty(req) || ObjectUtil.isEmpty(req.getPageNum()) || ObjectUtil.isEmpty(req.getPageSize())) {
            throw new BusinessException("必要参数不能为空");
        }
        LambdaQueryWrapper<ActivityApplyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityApplyEntity::getDeleted, 0L);
        wrapper.eq(StringUtil.isNotBlank(req.getId()), ActivityApplyEntity::getId, req.getId());
        wrapper.eq(StringUtil.isNotBlank(req.getActivityId()), ActivityApplyEntity::getActivityId, req.getActivityId());
        wrapper.eq(StringUtil.isNotBlank(req.getUserId()), ActivityApplyEntity::getUserId, req.getUserId());
        wrapper.eq(StringUtil.isNotBlank(req.getStatus()), ActivityApplyEntity::getStatus, req.getStatus());

        wrapper.orderByDesc(ActivityApplyEntity::getGmtCreateTime);

        Page<ActivityApplyEntity> page = page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
        if (ObjectUtil.isEmpty(page) || CollectionUtil.isEmpty(page.getRecords())) {
            return ResponsePageUtil.ok(req, new ArrayList<>(), 0L);
        }
        List<ActivityApplyResp> convert = BeanUtil.convert(page.getRecords(), ActivityApplyResp.class);
        return ResponsePageUtil.ok(req, convert, page.getTotal());

    }

    /**
     * 详情
     */
    @Override
    public ResponseBO<ActivityApplyResp> detail(ActivityApplyReq req) {
        if (ObjectUtil.isEmpty(req) || StringUtil.isBlank(req.getId())) {
            throw new BusinessException("必要参数不能为空");
        }
        ActivityApplyEntity entity = getById(req.getId());
        if (ObjectUtil.isEmpty(entity)) {
            throw new BusinessException("对象不存在");
        }
        return ResponseUtil.ok(BeanUtil.convert(entity, ActivityApplyResp.class));
    }

}